<template>
  <div class="app-container">
    <el-col :span="1.5">
      <el-button
        type="info"
        plain
        icon="el-icon-sort"
        size="mini"
        @click="toggleExpandAll"
      >展开/折叠</el-button>

      <el-date-picker
        v-model="value1"
        type="date"
        placeholder="选择日期"
        format="yyyy 年 MM 月 dd 日"
        value-format="yyyy-MM-dd">
      </el-date-picker>
      <el-button
        type="primary"
        plain
        icon="el-icon-search"
        size="mini"
        @click="fetchInitialData"
      >历史部门人数查询</el-button>
      <el-button
        type="primary"
        plain
        size="mini"
        @click="getList"
      >当前部门人数查询</el-button>
    </el-col>


    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      style="width: 100%"
      :max-height="maxHeight"
    >
      <el-table-column prop="deptName" label="部门名称" width="220" fixed></el-table-column>
      <el-table-column prop="deptId" label="部门编号" width="100" fixed></el-table-column>
      <el-table-column prop="totalCount" label="部门人数" align="center"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  getcountbygenderApi,
  getDataStatisticsApi,
  getDataStatisticsApi2,
  getEducationalLeveList,
  getRegionList,
  resetList
} from "@/api/Empment/DataStatistics";

export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // getDataStatisticsApi的返回数据
      deptData: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 视口高度
      viewportHeight: window.innerHeight,
      // 日期选择器的绑定值
      value1: null,
      // 定时器
      timer: null
    };
  },
  computed: {
    maxHeight() {
      return this.viewportHeight * 0.8; // 70% of the viewport height
    }
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    this.clearTimer();
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    /** 查询部门列表 */
    async getList() {
      this.loading = true;
      getDataStatisticsApi().then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
      });
    },
    /** 查询历史部门人数 */
    async fetchInitialData() {
      if (this.value1) {
        this.loading = true;
        getDataStatisticsApi2(this.value1).then(response => {
          this.deptList = this.handleTree(response.data, "deptId");
          this.loading = false;
        }).catch(error => {
          console.error("Error in fetchInitialData:", error);
        });
      } else {
        console.warn("还没有选择日期");
        this.getList();
      }
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 处理窗口大小变化 */
    handleResize() {
      this.viewportHeight = window.innerHeight;
    },
  }
};
</script>

